package jsyf.oa.dao.crm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.crm.CustomerVisitEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;


/**
 * Created by wangmin on 2018/1/15.
 */
@Repository
public class CustomerVisitDao extends BaseDao {
    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, CustomerVisitEntity.class);
    }

    public Integer getStateByUserId(String userId) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT COUNT(1) c FROM T_CRM_CUSTOMER_VISIT ");
        sb.append("WHERE CREATE_USER_ID='" + userId + "' ");
        sb.append("AND LEAVE_TIME IS NULL ");
        JSONObject jsonObject = queryForJsonObject(sb.toString());
        if (jsonObject == null)
            return 0;
        else
            return Integer.parseInt(jsonObject.get("c").toString());
    }

    public JSONObject getEntityByUserId(String userId) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT ID as id,CUSTOMER_NAME as customerName,WECHAT as wechat, ");
        sb.append("CUSTOMER_TEL as customerTel,ARRIVALS_TIME as arrivalsTime,  ");
        sb.append("LEAVE_TIME as leaveTime,LEAVE_IMG as leaveImg,CREATE_USER_ID as createUserId,  ");
        sb.append("ARRIVALS_LONGITUDE as arrivalsLongitude,ARRIVALS_LATITUDE as arrivalsLatitude,  ");
        sb.append("ARRIVALS_ADDRESS as arrivalsAddress,LEAVE_LONGITUDE as leaveLongitude,  ");
        sb.append("LEAVE_LATITUDE as leaveLatitude,LEAVE_ADDRESS as leaveAddress  ");
        sb.append("FROM T_CRM_CUSTOMER_VISIT ");
        sb.append("WHERE CREATE_USER_ID='" + userId + "' ");
        sb.append("AND LEAVE_TIME IS NULL ORDER BY LEAVE_TIME DESC LIMIT 1");
        JSONObject jsonObject = queryForJsonObject(sb.toString());
        return jsonObject == null ? null : jsonObject;
    }

    public Integer saveEntity(CustomerVisitEntity customerVisitEntity) {
        StringBuffer sb = new StringBuffer();
        sb.append("INSERT T_CRM_CUSTOMER_VISIT (ID,CUSTOMER_NAME,CUSTOMER_TEL,WECHAT,CREATE_USER_ID,ARRIVALS_LONGITUDE,ARRIVALS_LATITUDE,ARRIVALS_ADDRESS) ");
        sb.append("VALUES (");
        sb.append("'" + generateKey() + "','" + customerVisitEntity.getCustomerName() + "','" + customerVisitEntity.getCustomerTel() + "',");
        sb.append("'" + customerVisitEntity.getWeChat() + "','" + customerVisitEntity.getCreateUserId() + "',");
        sb.append("'" + customerVisitEntity.getLongitude() + "','" + customerVisitEntity.getLatitude() + "',");
        sb.append("'" + customerVisitEntity.getAddress() + "'");
        sb.append(")");
        return getJdbcTemplate().update(sb.toString());
    }

    public Integer updateEntity(CustomerVisitEntity customerVisitEntity) {
        StringBuffer sb = new StringBuffer();
        sb.append("UPDATE T_CRM_CUSTOMER_VISIT SET CUSTOMER_NAME='" + customerVisitEntity.getCustomerName() + "',");
        sb.append("CUSTOMER_TEL='" + customerVisitEntity.getCustomerTel() + "',");
        sb.append("WECHAT='" + customerVisitEntity.getWeChat() + "',");
        sb.append("LEAVE_IMG='" + customerVisitEntity.getLeaveImg() + "',");
        sb.append("LEAVE_TIME=now(), ");
        sb.append("LEAVE_IMG='" + customerVisitEntity.getLeaveImg() + "', ");
        sb.append("LEAVE_LONGITUDE='" + customerVisitEntity.getLongitude() + "', ");
        sb.append("LEAVE_LATITUDE='" + customerVisitEntity.getLatitude() + "', ");
        sb.append("LEAVE_ADDRESS='" + customerVisitEntity.getAddress() + "' ");
        sb.append("WHERE CREATE_USER_ID='" + customerVisitEntity.getCreateUserId() + "' ");
        sb.append("AND LEAVE_TIME IS NULL;");
        return getJdbcTemplate().update(sb.toString());
    }
}
